package com.bz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bz.common.PageInfo;
import com.bz.common.UserConfig;
import com.bz.dao.PreojectAreaDao;
import com.bz.dao.PreojectDao;
import com.bz.entity.PreojectAreaEntity;
import com.bz.entity.PreojectEntity;
import com.bz.entity.UserEntity;
import com.bz.service.IPreojectService;

@Service
public class PreojectServiceImpl implements IPreojectService {

    @Autowired
    private PreojectDao     preojectDao;

    @Autowired
    private PreojectAreaDao preojectAreaDao;

    @Override
    public PageInfo<PreojectEntity> list(PageInfo page, LambdaQueryWrapper<PreojectEntity> query) {
        query.orderByDesc(PreojectEntity::getUpdateDate);
        PageInfo<PreojectEntity> pd = preojectDao.selectPage(page, query);
        if (null == pd || CollectionUtils.isEmpty(pd.getRecords())) {
            return pd;
        }
        for (PreojectEntity d : pd.getRecords()) {
            PreojectAreaEntity area = preojectAreaDao.selectById(d.getAreaId());
            if (null != area) {
                d.setAreaName(area.getName());
            }
        }
        return pd;
    }

    @Override
    public void add(PreojectEntity data) {
        UserEntity lu = UserConfig.getLoginUser();
        data.setCreateBy(lu.getUsername());
        data.setCreateDate(new Date());
        data.setUpdateBy(lu.getUsername());
        preojectDao.insert(data);
    }

    @Override
    public void update(PreojectEntity data) {
        UserEntity lu = UserConfig.getLoginUser();
        data.setUpdateBy(lu.getUsername());
        preojectDao.updateById(data);
    }

    @Override
    public void delete(Long id) {
        preojectDao.deleteById(id);
    }

    @Override
    public List<PreojectAreaEntity> getArea(LambdaQueryWrapper<PreojectAreaEntity> query) {
        query.orderByAsc(PreojectAreaEntity::getUpdateDate);
        return preojectAreaDao.selectList(query);
    }

}
